<?php
add_action('widgets_init', 'reedwan_popular_posts_load_widgets');

function reedwan_popular_posts_load_widgets()
{
	register_widget('Reedwan_Popular_Posts_Widget');
}

class Reedwan_Popular_Posts_Widget extends WP_Widget {
	
	function Reedwan_Popular_Posts_Widget()
	{
		$widget_ops = array('classname' => 'reedwan_popular_posts_widget', 'description' => 'The most popular posts on your site');

		$control_ops = array('id_base' => 'reedwan_popular_posts_widget');

		$this->WP_Widget('reedwan_popular_posts_widget', 'Popular Posts With Thumbnail', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance)
	{
		extract($args);
		
		$title = $instance['title'];
		$post_type = 'all';
		$posts = $instance['posts'];
		$images = true;
		
		echo $before_widget;
		?>
		<!-- BEGIN WIDGET -->
		<?php
		if($title) {
			echo $before_title.$title.$after_title;
		}
		?>
		
		<?php
		$post_types = get_post_types();
		unset($post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
		
		if($post_type == 'all') {
			$post_type_array = $post_types;
		} else {
			$post_type_array = $post_type;
		}
		?>
		<?php
			$popularpost = new WP_Query( array( 'showposts' => $posts,'post_type' => $post_type_array, 'meta_key' => 'post_views_count', 'orderby' => 'post_views_count', 'order' => 'DESC'  ) );
			$counter = 1; 
			while($popularpost->have_posts()): $popularpost->the_post(); 
			if(has_post_format('video')) 
			{
				$format_icon = 'class="video-format-icon"';
				$format_icon_small = ' (Video)';
			}
			else if (has_post_format('audio'))
			{
				$format_icon = 'class="audio-format-icon"';
				$format_icon_small = ' (Audio)';
			}
			else if (has_post_format('gallery'))
			{
				$format_icon = 'class="gallery-format-icon"';
				$format_icon_small = ' (Gallery)';
			}
			else {
				$format_icon = 'class="standard-format-icon"';
				$format_icon_small = '';
			}
			
			$review_show =  get_post_meta(get_the_ID(), 'reedwan_show_review', true);
			$overall_percentage =  get_post_meta(get_the_ID(), 'reedwan_overall_score', true);

				
			?>
		<div class="block-small <?php if($counter == 1) {echo'no-margin';}?>">
				<?php if(has_post_thumbnail()): ?>
				<div class="magz-image small">
					<a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_post_thumbnail('small-magz', array('class' => 'overlay'));?></a>
					<?php if ($review_show == 'Yes' && $overall_percentage!=0) { ?>
						<span class="content-score"><?php echo $overall_percentage; ?>%</span>
					<?php } ?>
				</div>
				<?php else: ?>
				<div class="magz-image small">
					<a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo get_template_directory_uri(); ?>/images/thumbnails/no-image-small-magz.png" alt="no image" class="overlay"/></a>
					<?php if ($review_show == 'Yes' && $overall_percentage!=0) { ?>
						<span class="content-score"><?php echo $overall_percentage; ?>%</span>
					<?php } ?>
				</div>
				<?php endif; ?>
				<div class="small-desc">
					<h3 class="desc-title"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h3>
					<div class="magz-meta"><?php the_time('F j, Y'); ?>  |  <?php echo getPostViews(get_the_ID()); ?></div>
				
				</div>
		</div>
		<?php $counter++; endwhile; ?>
		<?php
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		
		$instance['title'] = $new_instance['title'];
		$instance['post_type'] = 'all';
		$instance['posts'] = $new_instance['posts'];
		$instance['show_images'] = true;
		
		return $instance;
	}

	function form($instance)
	{
		$defaults = array('title' => 'Popular Posts', 'post_type' => 'all', 'posts' => 5);
		$instance = wp_parse_args((array) $instance, $defaults); ?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('posts'); ?>">Number of posts:</label>
			<input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" value="<?php echo $instance['posts']; ?>" />
		</p>
		
	<?php 
	}
}
?>